package cn.com.utt.unms.controller;

import java.security.Principal;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import cn.com.utt.common.vo.RestResult;
import cn.com.utt.common.vo.TableInput;
import cn.com.utt.common.vo.TableOutput;
import cn.com.utt.common.vo.User;
import cn.com.utt.unms.pojo.Wireless;
import cn.com.utt.unms.service.WirelessService;
import cn.com.utt.unms.vo.WirelessInfoInput;
import cn.com.utt.unms.vo.WirelessInfo;

@RestController
@RequestMapping("v1/wirelesses")
public class WirelessController {

	@Autowired
	private WirelessService wirelessService;

	@PreAuthorize("hasRole('CONFIG_VIEW')")
	@RequestMapping(value = "/list", method = RequestMethod.POST, consumes = "application/json")
	@ResponseBody
	public Map<String, Object> list(@RequestBody TableInput input, Principal principal) {
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		RestResult result = new RestResult();
		TableOutput<WirelessInfo> output = wirelessService.queryByCondition(input, user.getParentId() == null?user.getId():user.getParentId());
		result.put("start", output.getStart());
		result.put("rows", output.getRows());
		result.put("total", output.getTotal());
		result.put("wirelesses", output.getList());
		if (user.hasRole("ROLE_CONFIG_EDIT")) {
			result.put("hasEditRole", true);
		} else {
			result.put("hasEditRole", false);
		}
		return result.toJsonObject();
	}

	@PreAuthorize("hasRole('CONFIG_EDIT')")
	@RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST, consumes = "application/json")
	@ResponseBody
	@Transactional
	public Map<String, Object> saveOrUpdate(@RequestBody WirelessInfoInput config, Principal principal) {
		RestResult result;
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		result = wirelessService.saveOrUpdate(config, user);
		return result.toJsonObject();
	}

	@PreAuthorize("hasRole('CONFIG_EDIT')")
	@RequestMapping(value = "/delete", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> delete(@RequestParam(value = "wirelessIds[]") List<Long> wirelessIds,
			Principal principal) {
		RestResult result;
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		result = wirelessService.delete(wirelessIds, user.getParentId() == null?user.getId():user.getParentId());
		return result.toJsonObject();
	}

	@PreAuthorize("hasRole('CONFIG_VIEW')")
	@RequestMapping(value = "/selectList", method = { RequestMethod.GET })
	public Map<String, Object> simplelist(Principal principal) {
		
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		
		RestResult result = new RestResult();
		List<Wireless> wirelesses = wirelessService.queryByUser(user.getParentId() == null?user.getId():user.getParentId());
		SimplePropertyPreFilter wirelessFilter = new SimplePropertyPreFilter(Wireless.class, "id", "name");
		result.put("wirelesses", JSON.toJSONString(wirelesses, wirelessFilter));
		return result.toJsonObject();
	}

}